package com.yunji.web.order;

import com.google.common.collect.Lists;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.order.bo.OrderUnFrozenBO;
import com.yunji.order.business.OrderBusiness;
import com.yunji.order.context.convert.OrderConvert;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.dto.OrderPageDTO;
import com.yunji.system.context.SystemContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2025/3/31 19:00
 * @description : TODO
 */
@Api(tags = "管理端-订单模块")
@RestController
@RequestMapping("/container/order")
public class OrderController extends BaseController {
    @Resource
    private OrderBusiness orderBusiness;

    @ApiOperation("解冻冻结金额-后端测试用")
    @GetMapping("/settlements/{orderId}")
    public Response<Boolean> settlements(@PathVariable("orderId") Long orderId) {
        OrderDO order = orderBusiness.getOrderById(orderId);
        OrderUnFrozenBO orderUnFrozenBO = OrderConvert.buildOrderScaleBO(order);
        orderBusiness.unFrozenScale(orderUnFrozenBO);
        return ResponseHelper.success();
    }


    @ApiOperation("查询订单列表")
    @PreAuthorize("@ss.hasPermi('container:order:query')")
    @GetMapping("/list")
    public Response<List<OrderDO>> list(@Valid OrderPageDTO param) {
        List<OrderDO> list = orderBusiness.orderScopeList(param);
        return ResponseHelper.successWithPage(list, param.getTotal());
    }

    @ApiOperation("导出订单列表")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('container:order:export')")
    @PostMapping("/export")
    public void export(@RequestBody OrderPageDTO param) {
        param.setExport(true);
        List<OrderDO> list = orderBusiness.orderScopeList(param);
        ExcelUtil<OrderDO> util = new ExcelUtil<>(OrderDO.class);
        List<String> fieldList = Lists.newArrayList("updateTime", "updateName", "remarks", "createBy",
                "createName", "updateBy", "createTime");
        if (!SystemContext.isPlate())
            fieldList.add("costPrice");
        if (SystemContext.isSite()) {
            fieldList.add("agentName");
            fieldList.add("agentPhone");
            fieldList.add("agentScale");
        }
        util.hideColumn(fieldList);
        util.exportExcel(response, list, "订单数据");
    }

    @ApiOperation("获取订单详细信息")
    @PreAuthorize("@ss.hasPermi('container:order:details')")
    @GetMapping(value = "/{id}")
    public Response<OrderDO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(orderBusiness.getOrderAndDetailNums(id));
    }

    @ApiOperation("修改订单")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public Response<String> edit(@RequestBody OrderDO order) {
        orderBusiness.updateOrder(order);
        return ResponseHelper.success();
    }
}
